package com.blackhorse.service;

import com.blackhorse.dao.AccountDao;
import com.blackhorse.domain.Account;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service("accountServiceImplAnno")
@Transactional(propagation = Propagation.REQUIRED, readOnly = false)//提供声明式事务支持
public class AccountServiceImplAnno implements AccountService {

    @Autowired
    private AccountDao accountDao;

    @Override
    public Account findAccountById(int id) {
        return accountDao.findAccountById(id);
    }

    @Override
    public void transfer(String sourceName, String targetName, float money) {
        Account srcAccount = accountDao.findAccountByName(sourceName);
        Account tarAccount = accountDao.findAccountByName(targetName);

        srcAccount.setMoney(srcAccount.getMoney() - money);
        tarAccount.setMoney(tarAccount.getMoney() + money);

        accountDao.updateAccount(srcAccount);
        //如果这里出现异常，转账金额将不再守恒
        int a = 1/0;
        accountDao.updateAccount(tarAccount);
    }
}
